package com.softer.wxzj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.softer.wxzj.common.entity.CommonResult;
import com.softer.wxzj.common.entity.Page;
import com.softer.wxzj.common.enums.NumberEnum;
import com.softer.wxzj.common.util.DateUtils;
import com.softer.wxzj.common.util.ShiroUserUtil;
import com.softer.wxzj.entity.AccRateInfo;
import com.softer.wxzj.mapper.AccCalculateInterestInfoMapper;
import com.softer.wxzj.mapper.AccRateInfoMapper;
import com.softer.wxzj.service.AccRateInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zcz
 * @date 2019/7/29
 */
@Service
public class AccRateInfoServiceImpl implements AccRateInfoService {

    @Autowired
    AccRateInfoMapper accRateInfoMapper;
    @Autowired
    AccCalculateInterestInfoMapper accCalculateInterestInfoMapper;
    @Override
    public CommonResult insert(AccRateInfo accRateInfo) {
        int count = accRateInfoMapper.getCheckCount(accRateInfo);
        if(count > NumberEnum.ZERO.getNum()){
            return  CommonResult.strResult(NumberEnum.ZERO.getNum(),"同银行、同利率类别起始日期不能有交集！");
        }
        accRateInfo.setCreater(ShiroUserUtil.getUserName());
        int i = accRateInfoMapper.insert(accRateInfo);
        return CommonResult.ResponseResult(i);
    }

    @Override
    public CommonResult update(AccRateInfo accRateInfo) {
        int count = accRateInfoMapper.getCheckCount(accRateInfo);
        //验证是否为当前结算日期
        String date = accRateInfoMapper.getRateDateByRateId(accRateInfo.getId());
        if(DateUtils.getMmDdNow().equals(date)){
            return  CommonResult.strResult(NumberEnum.ZERO.getNum(),"结算日当天不可修改结息利率！");
        }
        if(count > NumberEnum.ZERO.getNum()){
            return  CommonResult.strResult(NumberEnum.ZERO.getNum(),"同银行、同利率类别起始日期不能有交集！");
        }
        accRateInfo.setCreater(ShiroUserUtil.getUserName());
        int i = accRateInfoMapper.update(accRateInfo);
        return CommonResult.ResponseResult(i);
    }

    @Override
    public CommonResult delete(String id) {
        int count = accCalculateInterestInfoMapper.getCountByRateId(id);
        if(count > NumberEnum.ZERO.getNum()){
            return  CommonResult.strResult(NumberEnum.ZERO.getNum(),"当前结息已被使用，无法删除！如需删除请先删除对应结息配置！");
        }
        int i = accRateInfoMapper.delete(id);
        return  CommonResult.ResponseResult(i);
    }

    @Override
    public PageInfo<AccRateInfo> getList(Page page, AccRateInfo accRateInfo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<AccRateInfo> list = accRateInfoMapper.getList(accRateInfo);
        PageInfo<AccRateInfo> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public AccRateInfo getInfo(String id) {
        return accRateInfoMapper.getInfo(id);
    }
}
